package com.zz.bsea.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zz.bsea.model.User;
import com.zz.bsea.model.ViewProject;
import com.zz.bsea.service.ProjectService;
import com.zz.bsea.service.UserService;

@RestController
public class UserController {
	@Autowired
	UserService userService;
	
	@Autowired
	ProjectService projectService;
	@RequestMapping("query/{id}")
	public User queryUser(@PathVariable("id") long id ){
		
		return userService.selectByPrimaryKey(id);
	}

	
	@RequestMapping("queryAll")
	public List<User> queryAllUser(){
		
		return userService.getAllUser();
	}
	
	@RequestMapping("queryAllView")
	public List<ViewProject> getAllViewProj(){
		
		return userService.getAllViewProj();
	}
	
	@RequestMapping("queryAllView2")
	public List<ViewProject> getAllViewProj2(){
		
		return projectService.selectProview();
	}
	
	@RequestMapping("queryname/{name}")
	public User queryUserByName(@PathVariable("name") String name ){
		
		return userService.selectByName(name);
	}
	
	@RequestMapping("login")
	public Map<String,String>  getUserByName(@RequestBody User user,HttpServletRequest req){
		System.out.println(user.getName());
		User u2=userService.selectByName(user.getName());
		Map<String,String> m=new HashMap();
		if(u2==null){
			m.put("rs", "N");
			m.put("reason", "用户名不存在");
		}else if(!u2.getPwd().equals(user.getPwd())){
			m.put("rs", "N");
			m.put("reason", "密码不对");
		}else{
			String openid=(String) req.getSession().getAttribute("openId");
			System.out.println("--login-openid-"+openid);
			u2.setOpenid(openid);
			userService.updateByPrimaryKeySelective(u2);
			m.put("rs", "Y");
		}
		return m;
	}
	
	@RequestMapping("register")
	public Map<String,String>  addUer(@RequestBody User user,HttpServletRequest req){
		System.out.println(user.getName());
		String openid=(String) req.getSession().getAttribute("openId");
		user.setOpenid(openid);
		int number=userService.insertSelective(user);
		Map<String,String> m=new HashMap();
		if(number>0){
			m.put("rs", "Y");
		}else{
			m.put("rs", "N");
			m.put("reason", "注册失败");
		}
		return m;
	}
}
